function toggleMode() {
	if( document.getElementById( "SelectDirectory" ).hidden == true ) {
		document.getElementById( "SelectDirectory" ).hidden = false;
		document.getElementById( "AdvancedModeBox" ).hidden = true;
		document.getElementById( "Mode" ).label = "Switch to Advanced Mode";
		document.title = "Download Binning - Basic Mode";
	} else {
		document.getElementById( "SelectDirectory" ).hidden = true;
		document.getElementById( "AdvancedModeBox" ).hidden = false;
		document.getElementById( "Mode" ).label = "Switch to Basic Mode";
		document.title = "Download Binning - Advanced Mode";
	}
	window.sizeToContent();
}

function toggleDomainName() {
	if( document.getElementById( "DNRadios" ).disabled == true ) {
		document.getElementById( "DNRadios" ).disabled = false
		document.getElementById( "txtDNWildCard" ).disabled = false
		document.getElementById( "txtDNRegex" ).disabled = false
	} else {
		document.getElementById( "DNRadios" ).disabled = true
		document.getElementById( "txtDNWildCard" ).disabled = true
		document.getElementById( "txtDNRegex" ).disabled = true
	}
}

function toggleFileName() {
	if( document.getElementById( "FNRadios" ).disabled == true ) {
		document.getElementById( "FNRadios" ).disabled = false
		document.getElementById( "txtFNWildCard" ).disabled = false
		document.getElementById( "txtFNRegex" ).disabled = false
	} else {
		document.getElementById( "FNRadios" ).disabled = true
		document.getElementById( "txtFNWildCard" ).disabled = true
		document.getElementById( "txtFNRegex" ).disabled = true
	}
}

function openDirectoryPicker() {
	var nsIFilePicker = Components.interfaces.nsIFilePicker;
	var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
	fp.init(window, "Select a Directory", nsIFilePicker.modeGetFolder);
	var res = fp.show();
	if (res == nsIFilePicker.returnOK){
		var path = fp.file.path;
		document.getElementById( "saveDirectory" ).value = path;
	}
}

function onAdd() {
	var errorFound = false;
	var mode = 0;
	if( document.getElementById( "Mode" ).label == "Switch to Basic Mode" ) {
		//We are in advanced mode, there is more to do. 
		mode = 1;
		if( document.getElementById("saveDirectory").value == "" ) {
			alert( "Please Select a Directory." );
			errorFound = true;
		}
		if( document.getElementById("txtDNWildCard").disabled==true && 
				document.getElementById("txtFNWildCard").disabled==true ) {
			alert( "At least one filter box must be active and defined." );
			errorFound = true;
		}
		var DNErrorFound = checkDNValues();
		var FNErrorFound = checkFNValues();
		if( DNErrorFound || FNErrorFound ) {
			errorFound = true;
		}
		
		//Everything is filled out right, get the values and return.
		var filterName = document.getElementById("txtName").value;
		var filterDescription = document.getElementById("txtDescription").value;
		var dnexpression = "";
		var fnIsRegex = false;
		var dnIsRegex = false;
		var fnexpression = "";
		var directory = document.getElementById("saveDirectory").value;
		//Got the easy ones... find the expression.
		if( document.getElementById("txtDNWildCard").disabled==false ) {
			if( document.getElementById("DNRadios").selectedItem.label=="Wildcard"  ) {
				dnexpression = document.getElementById("txtDNWildCard").value; 
			} else {
				dnexpression = document.getElementById("txtDNRegex").value;
				dnIsRegex = true;
			}
		} 
		if( document.getElementById("txtFNWildCard").disabled==false ) {
			if( document.getElementById("FNRadios").selectedItem.label=="Wildcard" ) {
				fnexpression = document.getElementById("txtFNWildCard").value;
			} else {
				fnexpression = document.getElementById("txtFNRegex").value;
				fnIsRegex = true;
			}
		}
		window.arguments[0].out = {name:filterName,description:filterDescription,
				dnPattern:dnexpression,dnIsRegex:dnIsRegex,fnPattern:fnexpression,
				fnIsRegex:fnIsRegex,path:directory};
	} else {
		//Basic Mode, life is easy.
		if( document.getElementById("saveDirectory").value == "" ) {
			alert( "Please Select a Directory." );
			errorFound = true;
		} else {
			window.arguments[0].out = {name:"",description:"",dnPattern:"",dnIsRegex:false,
					fnPattern:document.getElementById("extensionMenu").selectedItem.value,
					fnIsRegex:false,path:document.getElementById("saveDirectory").value};
		}
	}
	//Save the mode!
	try {
		netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
	    var main = Components.classes['@downloadbinning.net/main;1'].getService(Components.interfaces.nsIDownloadBinning);
	    
	    main.setDefaultMode( mode );
	} catch (anError) {
		dump("ERROR: " + anError);
	    alert("Error: " + anError);
	}
	
	return !errorFound;
}

function checkDNValues() {
	var errorFound = false;
	
	if( document.getElementById("txtDNWildCard").disabled==false ) {
		if( document.getElementById("DNRadios").selectedItem.label=="Wildcard" &&
				document.getElementById("txtDNWildCard").value=="" ) {
			errorFound = true;
			alert( "Please Enter a Wildcard Value for the Domain Name" );
		} else if( document.getElementById("DNRadios").selectedItem.label=="Regular Expression" &&
				document.getElementById("txtDNRegex").value=="" ) {
			alert( "Please Enter a Regular Expression for the Domain Name" );
			errorFound = true;
		}
	}
	return errorFound;
}

function checkFNValues() {
	var errorFound = false;
	
	if( document.getElementById("txtFNWildCard").disabled==false ) {
		if( document.getElementById("FNRadios").selectedItem.label=="Wildcard" &&
				document.getElementById("txtFNWildCard").value=="" ) {
			alert( "Please Enter a Wildcard Value for the File Name" );
			errorFound = true;
		} else if( document.getElementById("FNRadios").selectedItem.label=="Regular Expression" &&
				document.getElementById("txtFNRegex").value=="" ) {
			alert( "Please Enter a Regular Expression for the File Name" );
			errorFound = true;
		}
	}
	return errorFound;
}

function init() {
	var mode;
	try {
		netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
	    var main = Components.classes['@downloadbinning.net/main;1'].getService(Components.interfaces.nsIDownloadBinning);
	    
	    mode = main.getDefaultMode();
	    if( mode != 0 ) {
	    	toggleMode();
	    } else {
	    	window.sizeToContent();
	    }
	} catch (anError) {
		dump("ERROR: " + anError);
	    alert("Error: " + anError);
	}
	
	if( window.arguments[0].inn != null ) {
	    var rule = window.arguments[0].inn.ruleToEdit;
	    
	    //see if we need Advanced Mode
	    if( rule.getName() == "" && rule.getDescription() == "" &&
	    		rule.getDomain() == "" && rule.getFilename() != "" ) {
	    	//Might be, now make sure that the File extension is in the combobox.
	    	var fileName = rule.getFilename();
	    	var extensionMenu = document.getElementById("extensionMenu");
	    	var found = false;
	    	for( var i = 0; i < extensionMenu.itemCount; i++ ) {	
	    		if( extensionMenu.getItemAtIndex(i).value == fileName ) {
	    			//Basic Mode is OK.
	    			found = true;
	    			extensionMenu.selectedIndex = i;
	    		}
	    	}
	    	if( mode != 0 ) {
	    		toggleMode();
	    	}
	    } else {
	    	//Advanced Mode is needed!
			document.getElementById( "txtName" ).value = rule.getName();
			document.getElementById( "txtDescription" ).value = rule.getDescription();
			if( rule.getDomain() == "" ) {
				document.getElementById( "DNR" ).checked = false;
				toggleDomainName();
			} else {
				if( rule.isDomainRegEx() ) {
					document.getElementById( "DNRadios" ).selectedIndex = 1;
					document.getElementById( "txtDNRegex" ).value = rule.getDomain();
				} else {
					document.getElementById( "txtDNWildCard" ).value = rule.getDomain();
				}
			}
			if( rule.getFilename() == "" ) {
				document.getElementById( "FNR" ).checked = false;
				toggleFileName();
			} else {
				if( rule.isFilenameRegEx() ) {
					document.getElementById( "FNRadios" ).selectedIndex = 1;
					document.getElementById( "txtFNRegex" ).value = rule.getFilename();
				} else {
					document.getElementById( "txtFNWildCard" ).value = rule.getFilename();
				}
			}
			if( mode == 0 ) {
	    		toggleMode();
	    	}
		} 
		document.getElementById("saveDirectory").value = rule.getDestination();
		
	}
}